package com.milk.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.milk.flux.actions.BaseLoadDataActionCreator;
import com.milk.flux.stores.BaseLoadDataStore;
import com.milk.flux.stores.Store;

import butterknife.ButterKnife;
import rx.Observable;

/**
 * Created by oceanzhang on 16/4/8.
 */
public abstract class BaseLoadDataFragment<T,S extends BaseLoadDataStore<T>,C extends BaseLoadDataActionCreator<T>> extends BaseFragment<S,C> {
    ViewStub viewStub;
    View contentView;
    FrameLayout emptyView;
    TextView tvErrorMsg;
    View progressView;
    protected  boolean isLoding;
    @Override
    protected final int getLayoutId() {
        return R.layout.fragment_base_load;
    }

    protected abstract @LayoutRes int getContentLayoutId();

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        initView(view);
        initData();
    }

    @Override
    public final void initView(View view) {
        super.initView(view);
        emptyView = (FrameLayout) view.findViewById(R.id.templet_empty_view);
        tvErrorMsg = (TextView) view.findViewById(R.id.act_templet_empty_tv_msg);
        progressView = view.findViewById(R.id.templet_empty_progress_view);
        viewStub = (ViewStub) view.findViewById(R.id.templet_content_view);
        viewStub.setLayoutResource(getContentLayoutId());
        contentView = viewStub.inflate();
        initContentView(contentView);

    }

    protected void initContentView(View view){
        ButterKnife.bind(this, view);
    }

    @Override
    protected boolean flux() {
        return true;
    }

    @Override
    public void initData() {
        super.initData();
        if(!isLoding) {
            startLoading();
            actionsCreator().loadData(createRequest());
            isLoding = true;
        }
    }

    @Override
    protected void updateView(Store.StoreChangeEvent event) {
        super.updateView(event);
        if(event.code == 200){
            if(event.error){
                loadError();
            }else{
                endLoading();
                bindView(store().getData());
            }
            isLoding = false;
        }

    }

    protected abstract void bindView(T t);
    protected abstract Observable<T> createRequest();
    public void startLoading(){
        progressView.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.VISIBLE);
        viewStub.setVisibility(View.GONE);
        tvErrorMsg.setVisibility(View.GONE);
    }
    public void endLoading(){
        emptyView.setVisibility(View.GONE);
        viewStub.setVisibility(View.VISIBLE);
        tvErrorMsg.setVisibility(View.GONE);
    }
    public void loadError(){
        progressView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        viewStub.setVisibility(View.GONE);
        tvErrorMsg.setVisibility(View.VISIBLE);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadAgain();
            }
        });
    }

    protected void loadAgain(){
        if(!isLoding) {
            isLoding = true;
            startLoading();
            actionsCreator().loadData(createRequest());
        }
    }
}
